package servlet;

import bean.Users;
import service.impl.UsersServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/login")
public class loginok extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String loginname = req.getParameter("TxtUserName");
        String password = req.getParameter("TxtPassword");
        UsersServiceimpl usersServiceimpl = new UsersServiceimpl();
        Users user = usersServiceimpl.isUserExist(loginname, password);

        if (user == null) {
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            writer.println("<script>location.href = 'login.jsp';alert('你的账号或是密码有误,请检查');</script>");
        }else {
            req.getSession().setAttribute("loginname",loginname);
            resp.sendRedirect("index.jsp");
        }

    }
}
